# coding:utf-8

from scrapy.spiders import CrawlSpider
import scrapy
from doubanmovie.items import MoviePic


class MovieSpiderClass(CrawlSpider):
    # 这是爬虫名
    name = 'douban_spider'
    # 允许的域名
    allowed_domains = ['movie.douban.com']
    # 入口url，扔到调度器里面去
    start_urls = ['https://movie.douban.com/top250']

    # 默认解析方法
    def parse(self, response):
        # 定义变量保存电影数据列表
        movie_list = response.xpath("//div[@class='article']//ol[@class='grid_view']/li")
        # 循环每一页电影条目
        for i_item in movie_list:

            # item文件导入
            douban_item = MoviePic()
            # 数据的处理
            douban_item['Movie_Id'] = i_item.xpath('.//div[@class="pic"]/em/text()').extract()
            # 写详细的xpath，进行数据的解析

            douban_item['Movie_Name'] = i_item.xpath(
                ".//div[@class='info']/div[@class='hd']/a/span[1]/text()").extract_first()

            douban_item['Movie_Img_URL'] = i_item.xpath('.//div[@class="pic"]/a/img/@src').extract()

            # 需要将数据yield到pipelines里面，然后进行数据的存储
            yield douban_item

        # 解析下一页规则，取后一页的xpath
        next_link = response.xpath("//span[@class='next']/link/@href").extract()
        if next_link:
            next_link = next_link[0]
            yield scrapy.Request('https://movie.douban.com/top250' + next_link, callback=self.parse)
